using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Home_AllocateStudentsToPlacements : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }


    #region Helper Functions
    protected string GetCampusName(string sPlacementTypeId)
    {
        PlacementManager.PlacementTypeComponent oPlacementTypeC = new PlacementManager.PlacementTypeComponent();
        int campusId = oPlacementTypeC.SelectOne(Int32.Parse(sPlacementTypeId)).CampusId;

        PlacementManager.CampusComponent oCampusC = new PlacementManager.CampusComponent();
        return oCampusC.SelectOne(campusId).CampusCode;

    }
    protected string GetUnitName(string unitId)
    {
        PlacementManager.UnitComponent oUnitC = new PlacementManager.UnitComponent();
        return oUnitC.SelectOne(Int32.Parse(unitId)).UnitCode + " - " + oUnitC.SelectOne(Int32.Parse(unitId)).UnitName;
    }
    protected string GetCategoryName(string categoryId)
    {
        PlacementManager.CategoryComponent oCategoryC = new PlacementManager.CategoryComponent();
        return oCategoryC.SelectOne(Int32.Parse(categoryId)).CategoryName;
    }
    protected bool CheckIfStudentsAllocated(string sVenuePlacementTypeId)
    {
        PlacementManager.AllocationComponent oAllocationC = new PlacementManager.AllocationComponent();
        List<PlacementManager.Allocation> oAllocationList = oAllocationC.SelectAllWVenuePlacementTypeId(Int32.Parse(sVenuePlacementTypeId), "VenuePlacementTypeId");
        if (oAllocationList.Count > 0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    protected string GetVenueDetails(string sVenuePlacementTypeId)
    {
        //Get the varaiables for this venue for this placement type
        int venuePlacementTypeId = Int32.Parse(sVenuePlacementTypeId);
        PlacementManager.LinkVenuePlacementTypeComponent oLvptC = new PlacementManager.LinkVenuePlacementTypeComponent();
        PlacementManager.LinkVenuePlacementType oLvpt = oLvptC.SelectOne(venuePlacementTypeId);
        int maxStudents = oLvpt.MaxNumStudents;
        bool isRestricted = oLvpt.IsRestricted;
        int venueId = oLvpt.VenueId;

        //Get the venue name
        PlacementManager.VenueComponent oVenueC = new PlacementManager.VenueComponent();
        string venueName = oVenueC.SelectOne(venueId).VenueName;
        if (venueName.Length > 30)
        {
            venueName = venueName.Substring(0, 31) + "...";
        }

        //Get the number of allocations
        PlacementManager.AllocationComponent oAllocationC = new PlacementManager.AllocationComponent();
        List<PlacementManager.Allocation> oAllocationList = oAllocationC.SelectAllWVenuePlacementTypeId(venuePlacementTypeId, "AllocationId");
        int numAllocated = oAllocationList.Count;

        string sToReturn = "<span style=\"font-weight:bold;\"><a href='Venues.aspx?id=" + venueId + "'>" + venueName + "</a></span>";
        sToReturn += "<br /><span class=\"gridViewSmallText\">Number Students Allocated:" + numAllocated.ToString();
        if (isRestricted)
        {
            sToReturn += " Max Num:" + maxStudents.ToString() + "</span>";

            if (numAllocated >= maxStudents)
            {
                sToReturn += "<br /><span class=\"message\">This venue is full.</span>";
            }
        }
        return sToReturn;
    }
    protected string GetStudentDetails(string sUserId)
    {
        PlacementManager.UserComponent oUserC = new PlacementManager.UserComponent();
        PlacementManager.User oUser = oUserC.SelectOne(Int32.Parse(sUserId));

        string s = oUser.IdNumber + " - " + oUser.LastName + ", " + oUser.FirstName;
        if (s.Length > 30)
        {
            s = s.Substring(0, 30) + "...";
        }
        return s;
    }
    #endregion


    protected void gvPlacementTypes_DataBound(object sender, EventArgs e)
    {
        this.gvAvailableStudents.DataBind();
        this.gvAvailableStudents.DataBind();
    }
    protected void gvPlacementTypes_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridViewRow gvr = e.Row;

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string toolTip = this.GetUnitName(DataBinder.Eval(e.Row.DataItem, "UnitId").ToString());
            toolTip += "<br /> Semester:" + DataBinder.Eval(e.Row.DataItem, "SemesterId").ToString();
            toolTip += "<br /> Year:" + DataBinder.Eval(e.Row.DataItem, "Year").ToString();
            toolTip += "<br /> Campus:" + this.GetCampusName(DataBinder.Eval(e.Row.DataItem, "PlacementTypeId").ToString());
            toolTip += "<br /> Category:" + this.GetCategoryName(DataBinder.Eval(e.Row.DataItem, "CategoryId").ToString());
            toolTip += "<br /> Start Date:" + (Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "DateStart"))).ToShortDateString();
            toolTip += "<br /> End Date:" + (Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "DateEnd"))).ToShortDateString();


            gvr.Cells[1].Attributes.Add("onmouseover", "showToolTip(event,'" + toolTip + "');");
            gvr.Cells[1].Attributes.Add("onmouseout", "hideToolTip();");
            gvr.Cells[2].Attributes.Add("onmouseover", "showToolTip(event,'" + toolTip + "');");
            gvr.Cells[2].Attributes.Add("onmouseout", "hideToolTip();");
            gvr.Cells[3].Attributes.Add("onmouseover", "showToolTip(event,'" + toolTip + "');");
            gvr.Cells[3].Attributes.Add("onmouseout", "hideToolTip();");



        }
    }
    protected void gvPlacementTypes_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.gvAvailableStudents.SelectedIndex = 0;
    }

    protected void gvAvailableStudents_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridViewRow gvr = e.Row;
        PlacementManager.AddressComponent oAddressC = new PlacementManager.AddressComponent();

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int addressId = oAddressC.SelectPrimaryAddressForUser(Int32.Parse(DataBinder.Eval(e.Row.DataItem, "UserId").ToString())).AddressId;
            string toolTip = DataBinder.Eval(e.Row.DataItem, "LastName").ToString();
            toolTip += ", " + DataBinder.Eval(e.Row.DataItem, "FirstName").ToString();
            toolTip += "<br /> Address:<br />" + oAddressC.GetFormattedAddress(addressId);

            gvr.Cells[1].Attributes.Add("onmouseover", "showToolTip(event,'" + toolTip + "');");
            gvr.Cells[1].Attributes.Add("onmouseout", "hideToolTip();");

        }
    }
    protected void btnRemoveCheckedStudents_Click(object sender, EventArgs e)
    {
        PlacementManager.AllocationComponent oAllocationC = new PlacementManager.AllocationComponent();
        int index = 0;
        int allocationId = 0;
        foreach (GridViewRow gvrVenues in this.gvVenues.Rows)
        {
            if (gvrVenues.RowType == DataControlRowType.DataRow)
            {
                GridView gvAllocatedStudents = (GridView)gvrVenues.FindControl("gvAllocatedStudents");
                if (gvAllocatedStudents != null)
                {
                    if (gvAllocatedStudents.Rows.Count > 0)
                    {
                        index = 0;
                        foreach (GridViewRow gvr in gvAllocatedStudents.Rows)
                        {
                            CheckBox RowLevelCheckBox = (CheckBox)gvr.FindControl("RowLevelCheckBox");
                            if (RowLevelCheckBox.Checked)
                            {
                                allocationId = Int32.Parse(gvAllocatedStudents.DataKeys[index].Value.ToString());
                                oAllocationC.Delete(allocationId);
                            }
                            index++;
                        }
                    }
                }
            }
        }

        this.gvAvailableStudents.DataBind();
        this.gvVenues.DataBind();
    }
    protected void gvVenues_DataBound(object sender, EventArgs e)
    {
        //If there are no venues allocated then we want to dsiable the add eductors button
        //and add students button
        if (this.gvVenues.Rows.Count > 0)
        {

            this.btnAddCheckedStudents.Enabled = true;

        }
        else
        {

            this.btnAddCheckedStudents.Enabled = false;

        }
    }
    protected void gvVenues_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void gvVenues_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        PlacementManager.LinkVenuePlacementTypeComponent oLvptC = new PlacementManager.LinkVenuePlacementTypeComponent();
        PlacementManager.LinkVenuePlacementType oLvpt = oLvptC.SelectOne(Int32.Parse(this.gvVenues.DataKeys[e.RowIndex].Value.ToString()));

        e.NewValues.Add("VenueId", oLvpt.VenueId);
        e.NewValues.Add("PlacementTypeId", oLvpt.PlacementTypeId);
        e.NewValues.Add("IsActive", oLvpt.IsActive);
        e.NewValues.Add("CreatedBy", oLvpt.CreatedBy);
        e.NewValues.Add("DateCreated", oLvpt.DateCreated);
        e.NewValues.Add("DateUpdated", DateTime.Now);
        e.NewValues.Add("UpdatedBy", User.Identity.Name);
    }
    protected void gvVenues_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
        e.KeepInEditMode = true;
    }
    protected void btnAddCheckedStudents_Click(object sender, EventArgs e)
    {
        this.AllocateCheckedStudents();
    }
    protected void gvVenues_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Button btnRemoveCheckedStudents = (Button)e.Row.FindControl("btnRemoveCheckedStudents");
            GridView gvAllocatedStudents = (GridView)e.Row.FindControl("gvAllocatedStudents");


            if (gvAllocatedStudents != null)
            {
                gvAllocatedStudents.DataBind();
                if (gvAllocatedStudents.Rows.Count == 0)
                {
                    btnRemoveCheckedStudents.Visible = false;
                }
            }
        }
    }
    protected void gvVenues_PageIndexChanged(object sender, EventArgs e)
    {

    }
    protected void gvVenues_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

    }
    protected void gvAvailableStudents_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.AllocateCheckedStudents();
    }

    protected void AllocateCheckedStudents()
    {
        if (this.gvVenues.SelectedIndex > -1)
        {
            PlacementManager.AllocationComponent oAllocationC = new PlacementManager.AllocationComponent();
            int index = 0;
            int userId = 0;

            foreach (GridViewRow gvr in this.gvAvailableStudents.Rows)
            {
                if (gvr.RowType == DataControlRowType.DataRow)
                {
                    CheckBox RowLevelCheckBox = (CheckBox)gvr.FindControl("RowLevelCheckBox");
                    if (RowLevelCheckBox.Checked)
                    {
                        userId = Int32.Parse(this.gvAvailableStudents.DataKeys[index].Value.ToString());
                        PlacementManager.Allocation oAllocation = new PlacementManager.Allocation();

                        oAllocation.CreatedBy = User.Identity.Name;
                        oAllocation.DateCreated = DateTime.Now;
                        oAllocation.DateUpdated = DateTime.Now;
                        oAllocation.CompletionStatusId = 4;
                        oAllocation.IsActive = true;
                        oAllocation.PlacementTypeId = Int32.Parse(this.gvPlacementTypes.SelectedValue.ToString());
                        oAllocation.UpdatedBy = User.Identity.Name;
                        oAllocation.UserId = userId;
                        oAllocation.VenuePlacementTypeId = Int32.Parse(this.gvVenues.SelectedDataKey.Value.ToString());
                        oAllocationC.Insert(oAllocation);

                    }
                }
                index++;
            }
            this.gvVenues.DataBind();
            this.gvAvailableStudents.DataBind();
        }
    }
    protected void ddUnitFilter_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.gvPlacementTypes.SelectedIndex = 0;
    }
    protected void ddCampusFilter_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.gvPlacementTypes.SelectedIndex = 0;
    }
    protected void ddSemesterFilter_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.gvPlacementTypes.SelectedIndex = 0;
    }
    protected void ddYearFilter_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.gvPlacementTypes.SelectedIndex = 0;
    }
}
